package com.briup.smartcity.service.impl;

import com.briup.smartcity.bean.Comment;
import com.briup.smartcity.exception.ServiceException;
import com.briup.smartcity.mapper.CommentMapper;
import com.briup.smartcity.mapper.extend.CommentExtendMapper;
import com.briup.smartcity.service.ICommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements ICommentService {
    @Autowired
    private CommentMapper mapper;
    @Autowired
    private CommentExtendMapper extendMapper;


    // 审核评论，状态：0-待审核，1-不通过，2-通过
    @Override
    public void checkComment(int id, int status) throws ServiceException {
        extendMapper.checkComment(id,status);
    }
    //分页查询评论信息
    @Override
    public PageInfo<Comment> pageQuery(String keywords,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize,true);
        List<Comment> list = extendMapper.PageQuery(keywords);
        PageInfo<Comment> commentPageInfo = new PageInfo<>(list);

        return commentPageInfo;
    }
    //通过id删除评论信息
    @Override
    public void deleteById(int id) throws ServiceException {
        List<Comment> list = extendMapper.selectParentId(id);

        if(list.size()>0){
            for(int i=0;i<list.size();i++) {
                deleteById(list.get(i).getCommentId());
            }
        }
        if(list == null){
            mapper.deleteByPrimaryKey(id);
        }
        mapper.deleteByPrimaryKey(id);
    }

    //批量删除评论
    @Override
    public void batchDelete(int[] ids) throws ServiceException {
        for (int i=0;i<ids.length;i++){
            deleteById(i);
        }
    }
}
